package com.randao.framework.utils;

import java.util.Calendar;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @Author: Zhangbx
 * @Description:
 * @Date: 2017/12/5
 * @Modified By:
 * @Modified Date:
 */
public class TimerUtil {

    public static long midTime;

    public static void main(String[] args) {
        timer1s(5);
//        timer2();
//        timer3();
//        timer4();


    }

    /**
     * 设定1000毫秒后执行
     */
    public static void timer1s(long endTime) {
        midTime = endTime;
        Timer nTimer = new Timer();
        nTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                System.out.println("验证码时间还剩：" + midTime-- + "秒");

                if (midTime == 0) {
                    nTimer.cancel();    //关闭
                    System.out.println("Redis中的验证码已失效");
                }
            }
        }, 0, 1000);

    }


    /**
     * 延迟5000毫秒，每1000毫秒执行一次
     */
    public static void timer1sDelay5s() {
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            public void run() {
                System.out.println("-------延迟5000毫秒，每1000毫秒执行一次--------");
            }
        }, 5000, 1000);
    }

    /**
     * 延迟5000毫秒，每1000毫秒执行一次
     */
    public static void timer1sDelay5sErr() {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                System.err.println("-------延迟5000毫秒，每1000毫秒执行一次--------");
            }
        }, 5000, 1000);
    }

    /**
     * 设置17：56执行任务
     * java.util.Timer.scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
     */
    public static void setTimeTask() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 17);
        calendar.set(Calendar.MINUTE, 26);
        calendar.set(Calendar.SECOND, 0);

        Date time = calendar.getTime();

        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                System.out.println("-------设定要指定任务--------");
            }
        }, time, 1000 * 60 * 60 * 24);// 这里设定将延时每天固定执行
    }


}


